package drugdealer.persistencia;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import drugdealer.dados.Funcionario;
import drugdealer.dados.Medico;
import drugdealer.persistencia.exceptions.AlreadyExistsException;
import drugdealer.persistencia.exceptions.DoesNotExistsException;

public class MedicoPersistence extends FuncionarioPersistence implements IMedicoPersistence {
	public static String TABLENAME = "medico";

	public MedicoPersistence(DDPersistence owner) {
		super(owner);
		this.tableName = MedicoPersistence.TABLENAME;
		this.primaryKey = MedicoPersistence.PRIMARYKEY;
		this.createTableStmt = "CREATE TABLE IF NOT EXISTS " + 
				MedicoPersistence.TABLENAME + "(" +
				MedicoPersistence.PRIMARYKEY + " varchar(11) PRIMARY KEY NOT NULL," +
				"crm varchar(40)" +
				")";
	}

	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IMedicoPersistence#get(java.lang.String)
	 */
	public Medico get(String cpf) {
		Medico medico = null;
		String selectMedico = "SELECT * FROM " + MedicoPersistence.TABLENAME + 
				" WHERE " + MedicoPersistence.PRIMARYKEY + "='" + cpf + "'";
		
		try {
			Statement s = this.ddc.createStatement();
			ResultSet rs = s.executeQuery(selectMedico);
			if (rs.next())
				medico = new Medico(super.get(cpf), rs.getString("crm"));
			rs.close ();
			s.close ();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return medico;
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IMedicoPersistence#getList()
	 */
	public Vector<String[]> getList() {
		Vector<String[]> list = new Vector<String[]>();
		String selectList = "SELECT p." + PacientePersistence.PRIMARYKEY + 
			",p.nome FROM " + PacientePersistence.TABLENAME  + " p," +
			MedicoPersistence.TABLENAME  + " m WHERE p." + 
			PacientePersistence.PRIMARYKEY + "=m." + MedicoPersistence.PRIMARYKEY;
		
		System.out.println(selectList);
		
		String[] pair;
		try {
			Statement s = this.ddc.createStatement();
			ResultSet rs = s.executeQuery(selectList);
			
			while (rs.next()) {
				pair = new String[2];
				pair[0] = rs.getString(PacientePersistence.PRIMARYKEY);
				pair[1] = rs.getString("nome");
				list.add(pair);
			}
			rs.close ();
			s.close ();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IMedicoPersistence#add(drugdealer.dados.Medico)
	 */
	public void add(Medico medico) throws AlreadyExistsException {
		super.add((Funcionario) medico);
		super.add("INSERT INTO " + MedicoPersistence.TABLENAME + " VALUES ('" + 
				medico.getCPF() + "','" + medico.getCRM() + "')");
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IMedicoPersistence#update(drugdealer.dados.Medico)
	 */
	public void update(Medico medico) throws DoesNotExistsException {
		super.update((Funcionario) medico);
		super.update("UPDATE " + MedicoPersistence.TABLENAME + " SET " +
				"crm='" + medico.getCRM() + "' " +
				"WHERE " + MedicoPersistence.PRIMARYKEY + "='" +
				medico.getCPF() + "'");
	}
	
	/* (non-Javadoc)
	 * @see drugdealer.persistencia.IMedicoPersistence#remove(drugdealer.dados.Medico)
	 */
	public void remove(Medico medico) {
		super.remove(MedicoPersistence.TABLENAME, 
				MedicoPersistence.PRIMARYKEY, medico.getCPF());
		super.remove((Funcionario) medico);
	}
}
